package dev.alm.javaproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dev.alm.javaproject.common.CommonResponse;
import dev.alm.javaproject.dto.UserAndDept;
import dev.alm.javaproject.dto.UserConditionAndPage;
import dev.alm.javaproject.entity.Order;
import dev.alm.javaproject.entity.User;
import dev.alm.javaproject.mapper.OrderMapper;
import dev.alm.javaproject.mapper.UserMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {

    @Resource
    UserMapper userMapper;

    @Resource
    OrderMapper orderMapper;

    @PostMapping("/login")
    public String login() {
        return "user login";
    }

    @PostMapping("/getUserById")
    public CommonResponse getUserById(@RequestBody User user) {
        Long id = user.getId();
        List<User> userById = userMapper.getUserById(id);
        CommonResponse cr = new CommonResponse();
        if (userById.size() > 0) {
            cr.success();
            cr.setData(userById);
        } else {
            cr.fail();
        }
        return cr;
    }

    @PostMapping("/getAllUserList")
    public CommonResponse getAllUserList() {
        CommonResponse cr = new CommonResponse();
        cr.success();
        cr.setData(userMapper.getAllUserList());
        return cr;
    }

    @PostMapping("/addUser")
    public CommonResponse addUser(@RequestBody User user) {
        // TODO
        // 通过雪花算法生成id
        CommonResponse cr = new CommonResponse();
        int i = userMapper.addUser(user);
        if (i > 0) {
            cr.success();
        } else {
            cr.fail();
        }
        return cr;
    }

    // 删除用户
    @PostMapping("/deleteUserById")
    public CommonResponse deleteUserById(@RequestBody User user) {
        Long id = user.getId();
        CommonResponse cr = new CommonResponse();
        int i = userMapper.deleteUserById(id);
        if (i > 0) {
            cr.success();
        } else {
            cr.fail();
        }
        return cr;
    }

    // 更新用户
    // 有问题
    @PostMapping("/updateUser")
    public CommonResponse updateUser(@RequestBody User user) {
        Long id = user.getId();
        List<User> userById = userMapper.getUserById(id);
        CommonResponse cr = new CommonResponse();
        if (userById.size() == 0) {
            // 未匹配到记录
            cr.fail();
            return cr;
        }
        User u = userById.get(0);
        String username = user.getUsername();
        String password = user.getPassword();
        if (!Strings.isEmpty(username)) {
            u.setUsername(username);
        }
        if (!Strings.isEmpty(password)) {
            u.setPassword(password);
        }
        System.out.println("u: " + u);
        int i = userMapper.updateUser(u);
        if (i > 0) {
            cr.success();
        } else {
            cr.fail();
        }
        return cr;
    }

    // 多表联合查询
    // 获取用户及其部门信息
    @PostMapping("/getUserAndDept")
    public CommonResponse getUserAndDepartment() {
        CommonResponse cr = new CommonResponse();
        cr.success();
        List<UserAndDept> userList = userMapper.getUserAndDepartment();
        cr.setData(userList);
        return cr;
    }

    // 多表查询
    // 获取用户及其订单信息（每个用户有多个订单）
    @PostMapping("/getUserAndOrder")
    public CommonResponse getUserAndOrder() {
        CommonResponse cr = new CommonResponse();
        List<User> allUserList = userMapper.getAllUserList();
        ArrayList<Long> longs = new ArrayList<>();
        for (User user : allUserList) {
            longs.add(user.getId());
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", longs);
        List<Order> orderList = orderMapper.selectList(wrapper);
        for (User user : allUserList) {
            ArrayList<Order> orders = new ArrayList<>();
            for (Order order : orderList) {
                if (user.getId().equals(order.getUserId())) {
                    orders.add(order);
                }
            }
            user.setOrders(orders);
        }
        cr.success();
        cr.setData(allUserList);
        return cr;
    }

}
